package com.ll.example.springbootwebdemokotlin.component

import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.ModelAndView
import java.lang.Exception
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 登陆拦截器
 */
class LoginHandlerInterceptor :HandlerInterceptor{
    //目标方法执行前
    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
        val user = request.session.getAttribute("loginuser")
        return if(user==null){
            //未登陆
            request.setAttribute("msg","没有权限请先登陆")
            request.getRequestDispatcher("/index.html").forward(request,response)
            false
        }else{
            //已登陆
            true
        }
    }
}